package com.csun.nusing.ui.familyactivity;

import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;

import com.csun.nusing.R;
import com.csun.nusing.familypresenter.FamilyMainPresenter;
import com.csun.nusing.presenter.inter.INursingMain;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.fragment.FamilyAlarmFragment;
import com.csun.nusing.ui.fragment.FamilyHomeFragment;
import com.csun.nusing.ui.fragment.FamilyMineFragment;
import com.csun.nusing.ui.fragment.FamilyOlderFragment;
import com.csun.nusing.util.MainHandlerConstant;
import com.csun.nusing.util.SPUtils;
import com.home.administrator.okhttplibrary.OkHttpTask;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 护工界面的主界面 底部4个tabbar 点击切换界面显示 ClassName: NursingMainActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-19
 */
public class FamilyMainActivity extends BaseActivity<FamilyMainPresenter>
		implements INursingMain, MainHandlerConstant {

	private FragmentManager fragmentManager;
	private RadioGroup bottomrg;
	private FamilyHomeFragment nursingHomeFragment;
	private FamilyAlarmFragment nursingAlarmFragment;
	private FamilyOlderFragment nursingOldsterFragment;
	private FamilyMineFragment nursingMineFragment;
	private OkHttpClient client;
	private String authorization;
	public static final MediaType JSON = MediaType
			.parse("application/json; charset=utf-8");

	@Override
	public int getContentViewId() {
		return R.layout.activity_family_main;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		fragmentManager = this.getSupportFragmentManager();
		bottomrg = findViewById(R.id.activity_familymain_bottom_rg);
		initFragment();
	}

	private void initFragment() {
		nursingHomeFragment = FamilyHomeFragment.newInstance();
		nursingAlarmFragment = FamilyAlarmFragment.newInstance();
		nursingOldsterFragment = FamilyOlderFragment.newInstance();
		nursingMineFragment = FamilyMineFragment.newInstance();
	}

	@Override
	public void initData() {
		super.initData();
		mPresenter.controlRadioGroup(bottomrg);

		authorization = (String) SPUtils.get(this, "authorization", "");
		String str = "{\"buildingid\": " + "0"
				+ ",\"buildingname\": \"\",\"roomid\": \"0\",\"roomno\": \"\"}";
		// 表单提交 这种能满足大部分的需求
		RequestBody requestBody = RequestBody.create(JSON, str);
		client = OkHttpTask.newinstance(this);
		Request request = new Request.Builder()
				.url("http://www.51cs8.com/NursingCloud/Bed/bedShowAll")
				.header("authorization", authorization)
				// 表单提交
				.post(requestBody).build();
		client.newBuilder().build().newCall(request).enqueue(new Callback() {

			@Override
			public void onResponse(Call arg0, Response response) throws IOException {
				Log.e("test","---------"+response.body().string());
			}
			@Override
			public void onFailure(Call arg0, IOException arg1) {
			}
		});

	}


	@Override
	protected FamilyMainPresenter getPresenter() {
		return new FamilyMainPresenter(this);
	}

	/**
	 * 底部tabbar的点击事件的回调
	 */
	@Override
	public void check(int rbFlag) {
		switch (rbFlag) {
		case 0:
			mPresenter.controlFragment(fragmentManager, nursingHomeFragment,
					"nursingHomeFragment");
			break;
		case 1:
			mPresenter.controlFragment(fragmentManager, nursingAlarmFragment,
					"nursingAlarmFragment");
			break;
		case 2:
			mPresenter.controlFragment(fragmentManager, nursingOldsterFragment,
					"nursingOldsterFragment");
			break;
		case 3:
			mPresenter.controlFragment(fragmentManager, nursingMineFragment,
					"nursingMineFragment");
			break;
		default:
			break;
		}
	}

}
